//获取应用实例
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    baseImgUrl: app.globalData.baseImgUrl,
    business: [],
    integral: 0, //积分
    sign_day: 0, //连续签到天数  0 1 2 3 4 5 6 
    isToday: false, //今天是否签到 
    sign_data: [], // 
    coupon_data: [], // 
  },

  // 
  onLoad: function (options) {
    // console.log(options);

    this.GetSignInfo(); // 获取签到信息
    this.GetCoupon(); // 请求优惠券



  },
  onShow: function () {
    this.setData({
      isLogin: app.globalData.isLogin
    })

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    var that = this;
    app.onLaunch(function () {
      that.onLoad();
    });
  },

  //立即签到
  Sign: function () {
    var that = this;
    if (!app.globalData.isLogin) {
      wx.navigateTo({
        url: '/pages/usercenter/userAuthorized/Authorized',
      });
      return false;
    }

    wx.request({
      url: app.globalData.Baseurl + 'user/click_sign',
      method: 'POST',
      data: {
        openid: app.globalData.openid || wx.getStorageSync('openid')
      },
      success: function (res) {
        wx.showToast({
          icon: 'none',
          title: res.data.msg,
        });

        if (res.data.status == 1) that.GetSignInfo(); // 获取签到信息
      }
    });


  },
  //积分记录
  integralLog: function () {

    wx.navigateTo({
      url: 'log',
    })


  },
  //请求优惠券
  GetCoupon: function () {
    var that = this;

    wx.request({
      url: app.globalData.Baseurl + 'user/conver_coupon_list',
      method: 'POST',
      data: {
        openid: app.globalData.openid || wx.getStorageSync('openid'),
        is_one: 1,
      },
      success: function (res) {
        if (res.data.status == 1) {
          for (var i in res.data.data) {

            res.data.data[i].s_time = that.timeFormat(res.data.data[i].s_time);
            res.data.data[i].e_time = that.timeFormat(res.data.data[i].e_time);

            res.data.data[i].img = app.globalData.BaseImgUrl + res.data.data[i].img;
          }
          that.setData({
            coupon_data: res.data.data
          });
        }
      }
    });

  },
  //积分兑换
  integralMall: function () {
    wx.navigateTo({
      url: '../integralMall/list',
    })
  },

  toDetail: function (e) {
    var id = e.currentTarget.dataset.id,
      type = e.currentTarget.dataset.type;

    wx.navigateTo({
      url: '../integralMall/detail?id=' + id + "&type=" + type,
    })
  },
  //兑换优惠券
  Exchange: function (e) {
    var that = this;
    console.log(e.currentTarget.dataset.id);

    wx.request({
      url: app.globalData.Baseurl + 'user/exchange_coupon',
      method: 'POST',
      data: {
        openid: app.globalData.openid || wx.getStorageSync('openid'),
        id: e.currentTarget.dataset.id,
      },
      success: function (res) {
        wx.showToast({
          icon: 'none',
          title: res.data.msg,
        });

        if (res.data.status == 1) that.GetCoupon(); // 请求优惠券
      }
    });

  },

  // 获取签到信息
  GetSignInfo: function () {
    var that = this;
    var inte = ['2', '2', '4', '4', '6', '6', '10'];


    //     验证手机号
    wx.request({
      url: app.globalData.Baseurl + 'user/sign_info',
      method: 'POST',
      data: {
        openid: app.globalData.openid || wx.getStorageSync('openid')
      },
      success: function (res) {
        var cur_time = new Date(new Date().setHours(0, 0, 0, 0)).getTime() / 1000;
        var is_today = false;
        var sign_data = [];

        // console.log(app.globalData.tel);

        if (res.data.status == 1) {

          for (var i = 0; i < 7; i++) {
            var item = {};

            if (i < res.data.data.sign_day) {
              item.class = 'signed';
            } else {
              item.class = 'unsign';

              if (res.data.data.vip == 2) item.text = '+' + 2 * inte[i];
              if (res.data.data.vip != 2) item.text = '+' + inte[i];
            }

            sign_data.push(item);
          }

          if (res.data.data.sign_time >= cur_time) {
            is_today = true;
          } else {
            sign_data[res.data.data.sign_day].class = sign_data[res.data.data.sign_day].class.replace("unsign", "signing");
          }

          that.setData({
            integral: res.data.data.integral,
            sign_day: res.data.data.sign_day,
            sign_data: sign_data,
            isToday: is_today
          });
        }

      }
    });
  },


  /**
   * 时间戳转换成 时间  年月日 
   */
  timeFormat: function (time) {
    if (!time || time == 'undefined') return '-';

    var d = new Date(1000 * time);
    var yy = d.getFullYear();
    var mm = d.getMonth() + 1;
    var dd = d.getDate();
    var hh = d.getHours();
    var ff = d.getMinutes();

    if (mm < 10) mm = "0" + mm;
    if (dd < 10) dd = "0" + dd;
    if (hh < 10) hh = "0" + hh;
    if (ff < 10) ff = "0" + ff;

    return yy + "." + mm + "." + dd;
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // }
})